import Vue from 'vue'
import Router from 'vue-router'
import store from '@/common/store/index.js'

Vue.use(Router)
const router = new Router({
  routes: [{
      path: '/',
      component: resolve => require(['../view/login.vue'], resolve),
      meta: {
        requireAuth: false
      }
    },
    {
      path: '/login',
      component: resolve => require(['../view/login.vue'], resolve),
      meta: {
        requireAuth: false
      }
    },
    {
      path: '/index',
      component: resolve => require(['../view/index.vue'], resolve),
      meta: {
        requireAuth: false
      },
      children: [],
    },
    {
      path: '/forgetPwd',
      component: resolve => require(['../view/forget_pwd.vue'], resolve),
      name: '忘记密码',
    },
    {
      path: '/otherLogin',
      component: resolve => require(['../view/other_login.vue'], resolve),
      name: '第三方绑定手机号',
    },
    {
      path: '/activity',
      component: resolve => require(['../view/activity.vue'], resolve),
      name: 'activity',
    },
    {
      path: '/connect',
      component: resolve => require(['../view/connect.vue'], resolve),
      name: 'connect',
    },
    {
      path: '/create',
      component: resolve => require(['../view/create.vue'], resolve),
      name: 'create',
    },
    {
      path: '/create_one',
      component: resolve => require(['../view/create_one.vue'], resolve),
      name: 'create_one',
    },
    {
      path: '/create_more',
      component: resolve => require(['../view/create_more.vue'], resolve),
      name: 'create_more',
    },
    {
      path: '/create',
      component: resolve => require(['../view/create.vue'], resolve),
      name: 'create',
    },
    {
      path: '/following',
      component: resolve => require(['../view/following.vue'], resolve),
      name: 'following',
    },
    {
      path: '/fq',
      component: resolve => require(['../view/fq.vue'], resolve),
      name: 'fq',
    },
    {
      path: '/detail',
      component: resolve => require(['../view/detail.vue'], resolve),
      name: 'detail',
    },
    {
      path: '/owned',
      component: resolve => require(['../view/owned.vue'], resolve),
      name: 'owned',
    },
    {
      path: '/space',
      component: resolve => require(['../view/space.vue'], resolve),
      name: 'space',
    },
    {
      path: '/collections',
      component: resolve => require(['../view/collections.vue'], resolve),
      name: 'collections',
    }

  ]
})
// 点击当前路由时
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 判断是否需要登录权限 以及是否登录
router.beforeEach((to, from, next) => {
  if (to.matched.some(res => res.meta.requireAuth)) { // 判断是否需要登录权限
    if (localStorage.getItem('token')) { // 判断是否登录
      next()
    } else {
      next({
        path: '/',
        query: {
          redirect: to.fullPath
        }
      })
    }
  } else {
    next()
  }
})

export default router
